package ua.kiev.tosik.price.gui;

import org.jdesktop.swingx.JXFrame;
import org.jdesktop.swingx.JXMonthView;
import org.jdesktop.swingx.JXPanel;
import ua.kiev.tosik.price.conf.AppConfig;
import ua.kiev.tosik.price.conf.Language;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Calendar;
import java.util.Locale;


public class CalendarWindow {

	private JXFrame calendarFrame;

	public CalendarWindow(String year) {
		Locale.setDefault(new Locale(AppConfig.getInstance().get("app.language")));
		showCalendarFrame(year);

	}

	public void showCalendarFrame(String year) {
		calendarFrame = new JXFrame(Language.getInstance().get("app.taskpanel.info.calendar") + " " + year);
		calendarFrame.setIconImage( new ImageIcon(Language.getInstance().get("app.taskpanel.info.calendar.icon")).getImage() );
		calendarFrame.setResizable(false);
		calendarFrame.setName("calendar" + year);
		calendarFrame.setDefaultCloseOperation(JXFrame.DO_NOTHING_ON_CLOSE);
		WindowListener l = new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				calendarFrame.removeWindowListener(this);
				// hideCalendarFrame();
				calendarFrame.setVisible(false);
				calendarFrame.dispose();
				// DemoUtils.fadeOutAndDispose(calendarFrame, 1000);
			}

		};
		calendarFrame.addWindowListener(l);
		JXPanel calendar = new JXPanel();
		calendar.setBackground(Color.WHITE);
		// Painter<?> painter = createBackgroundPainter();
		// calendar.setBackgroundPainter(painter);

		JXMonthView monthView = new JXMonthView();
		Calendar cal = monthView.getCalendar();
		cal.set(Calendar.YEAR, Integer.valueOf(year));
		cal.set(Calendar.MONTH, Calendar.JANUARY);
		monthView.setFirstDisplayedDay(cal.getTime());
		monthView.setOpaque(false);
		monthView.setPreferredColumnCount(3);
		monthView.setPreferredRowCount(4);
		// old style: set visual property with JXMonthView api
		monthView.setDayForeground(Calendar.SUNDAY, Color.MAGENTA);
		// <snip> Custom CalendarRenderingHandler
		// new style: install a custom renderingHandler
		// (as client property, because no public api support yet)
		// which allows to add Highlighters
		// monthView.putClientProperty("monthView.renderingHandler", createRenderingHandler());
		// </snip>
		// DemoUtils.setSnippet("Custom CalendarRenderingHandler", monthView);
		calendar.add(monthView);
		calendarFrame.add(calendar);
		calendarFrame.pack();
		calendarFrame.setLocationRelativeTo(MainWindow.getInstance().getFrame());
		calendarFrame.setVisible(true);
	}

}
